package com.macro.mall.product.service.impl;

import cn.hutool.core.util.StrUtil;
import com.macro.mall.common.api.CommonResult;
import com.macro.mall.common.exception.Asserts;
import com.macro.mall.common.utils.Assist;
import com.macro.mall.mapper.CommonMapper;
import com.macro.mall.model.Common;
import com.macro.mall.product.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Common的服务接口的实现类
 *
 * @author gaodx
 */
@Slf4j
@Service
public class CommonServiceImpl implements CommonService {

    // TODO 当你看到这个方法时你应该创建一个工具类做通用的方法,定义自己的返回格式化
    private static final int C200 = 200;
    private static final int C412 = 412;
    private boolean isDebugEnabled;
    @Autowired
    private CommonMapper commonMapper;

    @Override
    public List<Common> find(Common value) {
        //TODO这里可以做通过Assist做添加查询
        Assist assist = null;
        if (!StrUtil.isEmpty(value.getCommonType())) {
            assist = new Assist(Assist.whereRequire("and common_type = ", value.getCommonType()));
        }
        List<Common> result = commonMapper.selectCommon(assist);
        if (isDebugEnabled) {
            log.debug("执行获取Common数据集-->结果:", result);
        }
        return result;
    }

    @Override
    public Common findOne(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过Common的id获得Common对象-->失败:id不能为空");
            }
            Asserts.fail("执行通过Common的id获得Common对象-->失败:id不能为空");
        }
        Common result = commonMapper.selectCommonById(id);
        if (isDebugEnabled) {
            log.debug("执行通过Common的id获得Common对象-->结果:", result);
        }
        return result;
    }

    @Override
    public CommonResult saveNotNull(Common value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行将Common中属性值不为null的数据保存到数据库-->失败:对象不能为空");
            }
            Asserts.fail("失败:对象不能为空");
        }
        int result = commonMapper.insertNotNullCommon(value);
        if (isDebugEnabled) {
            log.debug("执行将Common中属性值不为null的数据保存到数据库-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult updateNotNullById(Common value) {
        if (value == null) {
            if (isDebugEnabled) {
                log.debug("执行通过Common的id更新Common中属性不为null的数据-->失败:对象为null");
            }
            Asserts.fail("执行通过Common的id更新Common中属性不为null的数据-->失败:对象为null");
        }
        int result = commonMapper.updateNotNullCommonById(value);
        if (isDebugEnabled) {
            log.debug("执行通过Common的id更新Common中属性不为null的数据-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public CommonResult deleteById(Integer id) {
        if (id == null) {
            if (isDebugEnabled) {
                log.debug("执行通过Common的id删除Common-->失败:id不能为空");
            }
            Asserts.fail("执行通过Common的id删除Common-->失败:id不能为空");
        }
        int result = commonMapper.deleteCommonById(id);
        if (isDebugEnabled) {
            log.debug("执行通过Common的id删除Common-->结果:", result);
        }
        return CommonResult.success(null);
    }

    @Override
    public List<Common> getKay() {
        List<Common> result = commonMapper.getKay();
        return result;
    }


}